package com.health.fragment.tijian;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.CheckBoxAdapter;
import com.health.adapter.RadioButtonAdapter;
import com.health.db.FDbManager;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_ASSESSMENTGUIDE;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.ARCHIVE_INOCULATIONHISTORY;
import com.health.tablebean.BD_DD_INFO;
import com.health.viewscontrols.tijian.TiJianActivity;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;
import com.health.widget.EditTextWithDate;
import com.health.widget.MyGridView;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 健康体检 健康评价
 * 
 * @author 鹿云龙
 */
public class HealthTiJian_JkpjFragment2 extends NovaFragment implements onFragmentSave, OnClickListener {
	private View baseView;
	private final static int I_NULL = -1;// 如果int类型的输入框为空
	private ARCHIVE_BASEINFO userInfo;// 基本信息
	// 非免疫预防接种史---------------------
	private List<BD_DD_INFO> lstName;
	private NiceSpinner spName1;
	private NiceSpinner spName2;
	private NiceSpinner spName3;

	private EditTextWithDate etw_date1;
	private EditTextWithDate etw_date2;
	private EditTextWithDate etw_date3;
	private Button chongzhi1;
	private Button chongzhi2;
	private Button chongzhi3;
	/** 其他机构1 */
	private EditText etINOCULATIONHISTORY1;
	/** 其他机构2 */
	private EditText etINOCULATIONHISTORY2;
	/** 其他机构3 */
	private EditText etINOCULATIONHISTORY3;
	// 健康评价
	private MyGridView mgvISNORMAL;
	private RadioButtonAdapter rbaISNORMAL;
	/** 异常1 */
	private EditText etEXCEPTION1;
	/** 异常2 */
	private EditText etEXCEPTION2;
	/** 异常3 */
	private EditText etEXCEPTION3;
	/** 异常4 */
	private EditText etEXCEPTION4;
	// 健康指导
	/** 健康指导 */
	private MyGridView mgvHEALTHZHIDAO;
	private CheckBoxAdapter cbaHEALTHZHIDAO;

	/** 危险因素控制 */
	private MyGridView mgvDANGERCONTROL;
	private CheckBoxAdapter cbaDANGERCONTROL;
	private ARCHIVE_INOCULATIONHISTORY ques1_before = new ARCHIVE_INOCULATIONHISTORY();
	private ARCHIVE_INOCULATIONHISTORY ques2_before = new ARCHIVE_INOCULATIONHISTORY();
	private ARCHIVE_INOCULATIONHISTORY ques3_before = new ARCHIVE_INOCULATIONHISTORY();
	RadioGroup rg_jkpj;
	RadioButton rb_wyc;
	RadioButton rb_yyc;

	/**
	 * 初始化控件数据
	 * 
	 * @author 鹿云龙
	 * @date 2015年12月21日 上午10:10:19
	 */
	private void initData() {
		// 疫苗
		List<ARCHIVE_INOCULATIONHISTORY> inos = FDbManager.getInstance(getActivity())
				.findAllByWhere(ARCHIVE_INOCULATIONHISTORY.class, "IDCARD = '" + userInfo.getIDCARD() + "'");
		int size = inos.size();
		if (size > 0) {
			for (int i = 0; i < size; i++) {
				if (i == 0) {
					inoform1 = 1;
					ques1_before = inos.get(0);
					spName1.setSelectedIndex(getListIndex(ques1_before.getPILLNAME(), lstName));
					etINOCULATIONHISTORY1.setText(ques1_before.getINOCULATIONHISTORY());
					if (ques1_before.getINOCULATIONDATE() != null) {
						String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(ques1_before.getINOCULATIONDATE());
						etw_date1.setText(dateStr);
					}
				} else if (i == 1) {
					inoform2 = 1;
					ques2_before = inos.get(1);
					spName2.setSelectedIndex(getListIndex(ques2_before.getPILLNAME(), lstName));
					etINOCULATIONHISTORY2.setText(ques2_before.getINOCULATIONHISTORY());
					if (ques2_before.getINOCULATIONDATE() != null) {
						String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(ques2_before.getINOCULATIONDATE());
						etw_date2.setText(dateStr);
					}
				} else if (i == 2) {
					inoform3 = 1;
					ques3_before = inos.get(2);
					spName3.setSelectedIndex(getListIndex(ques3_before.getPILLNAME(), lstName));
					etINOCULATIONHISTORY3.setText(ques3_before.getINOCULATIONHISTORY());
					if (ques3_before.getINOCULATIONDATE() != null) {
						String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(ques3_before.getINOCULATIONDATE());
						etw_date3.setText(dateStr);
					}
				}
			}
		}

		//////////////////////////////////////////////////////////////////////////
		List<ARCHIVE_ASSESSMENTGUIDE> asss = FDbManager.getInstance(getActivity())
				.findAllByWhere(ARCHIVE_ASSESSMENTGUIDE.class, "IDCARD = '" + userInfo.getIDCARD() + "'");
		if (asss.size() == 0) {
			return;
		}
		ARCHIVE_ASSESSMENTGUIDE ass = asss.get(asss.size() - 1);
		etEXCEPTION1.setText(ass.getEXCEPTION1());
		etEXCEPTION2.setText(ass.getEXCEPTION2());
		etEXCEPTION3.setText(ass.getEXCEPTION3());
		etEXCEPTION4.setText(ass.getEXCEPTION4());

		cbaHEALTHZHIDAO.setChecked(ass.getHEALTHZHIDAO());// 健康指导
		// rbaISNORMAL.setChecked(ass.getISNORMAL());// 有无异常
		if (!TextUtils.isEmpty(ass.getISNORMAL())) {
			if ("DM100-32_1".equals(ass.getISNORMAL())) {
				rb_wyc.setChecked(true);
				etEXCEPTION1.setEnabled(false);
				etEXCEPTION1.setText("");
				etEXCEPTION2.setEnabled(false);
				etEXCEPTION2.setText("");
				etEXCEPTION3.setEnabled(false);
				etEXCEPTION3.setText("");
				etEXCEPTION4.setEnabled(false);
				etEXCEPTION4.setText("");
			} else if ("DM100-32_2".equals(ass.getISNORMAL())) {
				rb_yyc.setChecked(true);
				etEXCEPTION1.setEnabled(true);
				etEXCEPTION2.setEnabled(true);
				etEXCEPTION3.setEnabled(true);
				etEXCEPTION4.setEnabled(true);
			}
		}
		cbaDANGERCONTROL.setChecked(ass.getDANGERCONTROL());
		cbaDANGERCONTROL.setADVICE(ass.getADVICE());
		cbaDANGERCONTROL.setAIM(ass.getAIM());
		cbaDANGERCONTROL.setOther(ass.getOTHER());

	}

	/**
	 * 保存数据
	 * 
	 * @author 鹿云龙
	 * @date 2015年12月21日 上午10:49:31
	 */
	private void saveData() {
		// INOCULATIONHISTORY
		/////////// 判断必填
		String str_id = lstName.get(spName1.getSelectedIndex()).getID();
		if (!TextUtils.isEmpty(etw_date1.getText()) || !TextUtils.isEmpty(etINOCULATIONHISTORY1.getText())||!TextUtils.isEmpty(str_id)) {
			if (TextUtils.isEmpty(etw_date1.getText())) {
				((TiJianActivity) getActivity()).showTextDialog("请填写接种日期");
				return;
			} else if (TextUtils.isEmpty(etINOCULATIONHISTORY1.getText())) {
				((TiJianActivity) getActivity()).showTextDialog("请填写接种机构");
				return;
			}
		}
		String str_id2 = lstName.get(spName2.getSelectedIndex()).getID();
		if (!TextUtils.isEmpty(etw_date2.getText()) || !TextUtils.isEmpty(etINOCULATIONHISTORY2.getText())||!TextUtils.isEmpty(str_id2)) {
			if (TextUtils.isEmpty(etw_date2.getText())) {
				((TiJianActivity) getActivity()).showTextDialog("请填写接种日期");
				return;
			} else if (TextUtils.isEmpty(etINOCULATIONHISTORY2.getText())) {
				((TiJianActivity) getActivity()).showTextDialog("请填写接种机构");
				return;
			}
		}
		String str_id3 = lstName.get(spName3.getSelectedIndex()).getID();
		if (!TextUtils.isEmpty(etw_date3.getText()) || !TextUtils.isEmpty(etINOCULATIONHISTORY3.getText())||!TextUtils.isEmpty(str_id3)) {
			if (TextUtils.isEmpty(etw_date3.getText())) {
				((TiJianActivity) getActivity()).showTextDialog("请填写接种日期");
				return;
			} else if (TextUtils.isEmpty(etINOCULATIONHISTORY3.getText())) {
				((TiJianActivity) getActivity()).showTextDialog("请填写接种机构");
				return;
			}
		}

		//////////////////////////////////////

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
		ARCHIVE_INOCULATIONHISTORY ino1 = new ARCHIVE_INOCULATIONHISTORY();
		ino1.setINOCULATIONHISTORY(etINOCULATIONHISTORY1.getText().toString());
		ino1.setPILLNAME(lstName.get(spName1.getSelectedIndex()).getID());
		ino1.setIDCARD(userInfo.getIDCARD());
		try {
			java.util.Date date = sdf.parse(etw_date1.getText().toString());
			ino1.setINOCULATIONDATE(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		ARCHIVE_INOCULATIONHISTORY ino2 = new ARCHIVE_INOCULATIONHISTORY();
		ino2.setINOCULATIONHISTORY(etINOCULATIONHISTORY2.getText().toString());
		ino2.setPILLNAME(lstName.get(spName2.getSelectedIndex()).getID());
		ino2.setIDCARD(userInfo.getIDCARD());
		try {
			java.util.Date date = sdf.parse(etw_date2.getText().toString());
			ino2.setINOCULATIONDATE(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		ARCHIVE_INOCULATIONHISTORY ino3 = new ARCHIVE_INOCULATIONHISTORY();
		ino3.setINOCULATIONHISTORY(etINOCULATIONHISTORY3.getText().toString());
		ino3.setPILLNAME(lstName.get(spName3.getSelectedIndex()).getID());
		ino3.setIDCARD(userInfo.getIDCARD());
		try {
			java.util.Date date = sdf.parse(etw_date3.getText().toString());
			ino3.setINOCULATIONDATE(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		ARCHIVE_ASSESSMENTGUIDE ass = new ARCHIVE_ASSESSMENTGUIDE();
		ass.setIDCARD(userInfo.getIDCARD());
		ass.setEXCEPTION1(etEXCEPTION1.getText().toString());
		ass.setEXCEPTION2(etEXCEPTION2.getText().toString());
		ass.setEXCEPTION3(etEXCEPTION3.getText().toString());
		ass.setEXCEPTION4(etEXCEPTION4.getText().toString());

		ass.setHEALTHZHIDAO(cbaHEALTHZHIDAO.getCheckedIdsStr());// 健康指导

		// ass.setISNORMAL(rbaISNORMAL.getChecked());// 有无异常
		if (rb_wyc.isChecked()) {
			ass.setISNORMAL("DM100-32_1");
		} else if (rb_yyc.isChecked()) {
			ass.setISNORMAL("DM100-32_2");
		} 

		ass.setDANGERCONTROL(cbaDANGERCONTROL.getCheckedIdsStr());
		ass.setADVICE(cbaDANGERCONTROL.getADVICE());
		ass.setAIM(cbaDANGERCONTROL.getAIM());
		ass.setOTHER(cbaDANGERCONTROL.getOther());
		if (assfrom == 0) {
			FDbManager.getInstance(getActivity()).save(ass);
			assfrom = 1;
		} else {
			FDbManager.getInstance(getActivity()).updata(ass, "IDCARD='" + userInfo.getIDCARD() + "'");
		}

		if (inoform1 == 0) {
			ino1.setNGUID(UUID.randomUUID().toString());
			FDbManager.getInstance(getActivity()).save(ino1);
			inoform1 = 1;
			ques1_before = ino1;

		} else {
			if (!ino1.equals(ques1_before)) {
				ino1.setNGUID(UUID.randomUUID().toString());
				FDbManager.getInstance(getActivity()).updata(ino1, "IDCARD='" + userInfo.getIDCARD() + "'  and ID=1");
				ques1_before = ino1;
			}
		}
		if (inoform2 == 0) {
			ino2.setNGUID(UUID.randomUUID().toString());
			FDbManager.getInstance(getActivity()).save(ino2);
			inoform2 = 1;
			ques2_before = ino2;

		} else {
			if (!ino2.equals(ques2_before)) {
				ino2.setNGUID(UUID.randomUUID().toString());
				FDbManager.getInstance(getActivity()).updata(ino2, "IDCARD='" + userInfo.getIDCARD() + "'  and ID=2");
				ques2_before = ino2;
			}

		}
		if (inoform3 == 0) {
			ino3.setNGUID(UUID.randomUUID().toString());
			FDbManager.getInstance(getActivity()).save(ino3);
			inoform3 = 1;
			ques3_before = ino3;
		} else {
			if (!ino3.equals(ques3_before)) {
				ino3.setNGUID(UUID.randomUUID().toString());
				FDbManager.getInstance(getActivity()).updata(ino3, "IDCARD='" + userInfo.getIDCARD() + "'  and ID=3");
				ques3_before = ino3;
			}
		}

		if (rb_yyc.isChecked()) {
			if (TextUtils.isEmpty(etEXCEPTION1.getText().toString())
					&& TextUtils.isEmpty(etEXCEPTION2.getText().toString())
					&& TextUtils.isEmpty(etEXCEPTION3.getText().toString())
					&& TextUtils.isEmpty(etEXCEPTION4.getText().toString())) {
				((TiJianActivity) getActivity()).showTextDialog("请填写健康评价异常情况");
				return;
			}
		}

		((TiJianActivity) getActivity()).showTextDialog("保存成功");
	}

	private int inoform1 = 0;
	private int inoform2 = 0;
	private int inoform3 = 0;
	private int assfrom = 0;
	// x片

	// 其他

	@SuppressLint("InflateParams")
	@Override
	public View findViews(LayoutInflater inflater) {
		baseView = inflater.inflate(R.layout.fragment_healthtijian_jkpj, null);
		userInfo = NovaApplication.getUserInfo();
		chongzhi1 = (Button) baseView.findViewById(R.id.chongzhi1);
		chongzhi2 = (Button) baseView.findViewById(R.id.chongzhi2);
		chongzhi3 = (Button) baseView.findViewById(R.id.chongzhi3);
		spName1 = (NiceSpinner) baseView.findViewById(R.id.sp_name1);
		spName2 = (NiceSpinner) baseView.findViewById(R.id.sp_name2);
		spName3 = (NiceSpinner) baseView.findViewById(R.id.sp_name3);

		etw_date1 = (EditTextWithDate) baseView.findViewById(R.id.etw_date1);
		etw_date2 = (EditTextWithDate) baseView.findViewById(R.id.etw_date2);
		etw_date3 = (EditTextWithDate) baseView.findViewById(R.id.etw_date3);

		mgvISNORMAL = (MyGridView) baseView.findViewById(R.id.mgv_tijianyichang);
		etINOCULATIONHISTORY1 = (EditText) baseView.findViewById(R.id.et_jigou1);
		etINOCULATIONHISTORY2 = (EditText) baseView.findViewById(R.id.et_jigou2);
		etINOCULATIONHISTORY3 = (EditText) baseView.findViewById(R.id.et_jigou3);
		etEXCEPTION1 = (EditText) baseView.findViewById(R.id.et_yichang1);
		etEXCEPTION2 = (EditText) baseView.findViewById(R.id.et_yichang2);
		etEXCEPTION3 = (EditText) baseView.findViewById(R.id.et_yichang3);
		etEXCEPTION4 = (EditText) baseView.findViewById(R.id.et_yichang4);
		mgvHEALTHZHIDAO = (MyGridView) baseView.findViewById(R.id.mgv_jiankangzhidao);
		mgvDANGERCONTROL = (MyGridView) baseView.findViewById(R.id.mgv_weixianyinsukongzhi);

		rg_jkpj = (RadioGroup) baseView.findViewById(R.id.rg_jkpj);
		rb_wyc = (RadioButton) baseView.findViewById(R.id.rb_wyc);
		rb_yyc = (RadioButton) baseView.findViewById(R.id.rb_yyc);

		GlobalConts.setMaxLength(35, etINOCULATIONHISTORY1, etINOCULATIONHISTORY2, etINOCULATIONHISTORY3);
		GlobalConts.setMaxLength(75, etEXCEPTION1, etEXCEPTION2, etEXCEPTION3, etEXCEPTION4);

		etw_date1.setBefore(new Date());
		etw_date2.setBefore(new Date());
		etw_date3.setBefore(new Date());
		// 疫苗其他35
		// 健康指导其他50
		// 减体重其他25
		bind1();
		initData();

		return baseView;
	}

	private void bind1() {
		// 非免疫预防接种史
		lstName = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class, "parentid='DM08-01'");
		BD_DD_INFO bd = new BD_DD_INFO();
		bd.setDATAINFO("无");
		bd.setID("");
		lstName.add(0,bd);
		ArrayList<String> datainfo = getDataInfo(lstName);
		spName1.attachDataSource(datainfo);
		spName2.attachDataSource(datainfo);
		spName3.attachDataSource(datainfo);
		// 健康评价
		List<BD_DD_INFO> pingjia = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
				"parentid='DM100-32' order by cast(SORT as int) asc");
		rbaISNORMAL = new RadioButtonAdapter(pingjia, getActivity());
		mgvISNORMAL.setAdapter(rbaISNORMAL);

		// mgvYiChang
		List<BD_DD_INFO> zhidao = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
				"parentid='DM100-33' order by cast(SORT as int) asc");

		List<BD_DD_INFO> listZZs_new = new ArrayList<BD_DD_INFO>();
		int size = zhidao.size();
		if (size > 0) {
			for (int i = 0; i < size; i++) {

				// DM100-33_1 纳入慢性疾病患者健康管理 DM100-33_2 建议复查 DM100-33_3 建议转诊
				// DM100-33_4 定期随访
				if (zhidao.get(i).getID().equals("DM100-33_1") || zhidao.get(i).getID().equals("DM100-33_2")
						|| zhidao.get(i).getID().equals("DM100-33_3")) {
					listZZs_new.add(zhidao.get(i));
				}
			}
		}

		cbaHEALTHZHIDAO = new CheckBoxAdapter(getActivity(), listZZs_new);
		mgvHEALTHZHIDAO.setAdapter(cbaHEALTHZHIDAO);

		List<BD_DD_INFO> kongzhi = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
				"parentid='DM100-34' order by cast(SORT as int) asc");
		cbaDANGERCONTROL = new CheckBoxAdapter(getActivity(), kongzhi);
		mgvDANGERCONTROL.setAdapter(cbaDANGERCONTROL);

	}

	/**
	 * 获取下拉框中需要的数据的信息
	 * 
	 * @param datas
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月16日 下午3:47:00
	 */
	@SuppressWarnings("unused")
	private ArrayList<String> getDataInfo(List<BD_DD_INFO> datas) {
		ArrayList<String> infos = new ArrayList<String>();
		for (BD_DD_INFO bd : datas) {
			infos.add(bd.getDATAINFO());
		}
		return infos;
	}

	@Override
	public void setupView(View view) {
	}

	@Override
	public void addListener() {
		chongzhi1.setOnClickListener(this);
		chongzhi2.setOnClickListener(this);
		chongzhi3.setOnClickListener(this);
		rb_yyc.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					etEXCEPTION1.setEnabled(true);
					etEXCEPTION2.setEnabled(true);
					etEXCEPTION3.setEnabled(true);
					etEXCEPTION4.setEnabled(true);
				} else {
					etEXCEPTION1.setEnabled(false);
					etEXCEPTION1.setText("");
					etEXCEPTION2.setEnabled(false);
					etEXCEPTION2.setText("");
					etEXCEPTION3.setEnabled(false);
					etEXCEPTION3.setText("");
					etEXCEPTION4.setEnabled(false);
					etEXCEPTION4.setText("");
				}
			}
		});
	}
	@Override
	public void setData() {
	}

	public int getListIndex(String id, List<BD_DD_INFO> bds) {
		int index = 0;
		try {
			for (int i = 0; i < bds.size(); i++) {
				if (id.equals(bds.get(i).getID())) {
					index = i;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return index;
	}

	/**
	 * 字符串转换"1,2,3,4,5" --> "'1','2','3','4','5'"
	 * 
	 * @param symptom
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月19日 下午2:49:23
	 */
	public String checkedIdsSql(String symptom) {
		String[] idarr = symptom.split(",");
		String sql = "";
		for (String id : idarr) {
			sql += "'" + id + "',";
		}
		sql = sql.substring(0, sql.length() - 1);
		System.out.println(sql);
		return sql;
	}

	@Override
	public void save() {
		saveData();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.chongzhi1) {
			// TODO 2017-9-23 17:33:58 必填验证
			etw_date1.setText("");
			etINOCULATIONHISTORY1.setText("");
		} else if (v.getId() == R.id.chongzhi2) {
			etw_date2.setText("");
			etINOCULATIONHISTORY2.setText("");
		} else if (v.getId() == R.id.chongzhi3) {
			etw_date3.setText("");
			etINOCULATIONHISTORY3.setText("");
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
//		bind1();
//		initData();
	}

}